/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dacnpm;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.logging.*;
import java.util.*;
import javax.swing.JOptionPane;

/**
 *
 * @author SONY
 */
public class Form_QuanLyUser extends javax.swing.JFrame {
    int admin=0;

    /**
     * Creates new form Form_QuanLyUser
     */
    public Form_QuanLyUser() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        tf21_username = new javax.swing.JTextField();
        tf21_password = new javax.swing.JPasswordField();
        cb21_admin = new javax.swing.JCheckBox();
        bt21_create = new javax.swing.JButton();
        bt21_logout = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setFocusCycleRoot(true);
        jPanel1.setPreferredSize(new java.awt.Dimension(800, 600));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
        jPanel1.add(tf21_username, new org.netbeans.lib.awtextra.AbsoluteConstraints(470, 300, 220, 50));
        jPanel1.add(tf21_password, new org.netbeans.lib.awtextra.AbsoluteConstraints(470, 370, 220, 50));

        cb21_admin.setText("Chọn nếu bạn muốn Tạo thêm người quản trị");
        cb21_admin.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cb21_adminItemStateChanged(evt);
            }
        });
        cb21_admin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cb21_adminActionPerformed(evt);
            }
        });
        jPanel1.add(cb21_admin, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 260, 290, -1));

        bt21_create.setText("Create");
        bt21_create.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt21_createActionPerformed(evt);
            }
        });
        jPanel1.add(bt21_create, new org.netbeans.lib.awtextra.AbsoluteConstraints(520, 440, 130, 50));

        bt21_logout.setText("Back");
        bt21_logout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt21_logoutActionPerformed(evt);
            }
        });
        jPanel1.add(bt21_logout, new org.netbeans.lib.awtextra.AbsoluteConstraints(703, 570, 80, 30));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/dacnpm/mslide2_1.jpg"))); // NOI18N
        jLabel1.setAutoscrolls(true);
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 800, 600));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bt21_logoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt21_logoutActionPerformed
        this.setVisible(false);
        Form_QuanTri t;
        try {
            t = new Form_QuanTri();
            t.setVisible(true);
        } catch (Exception ex) {
            Logger.getLogger(Form_QuanLyUser.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }//GEN-LAST:event_bt21_logoutActionPerformed

    private void bt21_createActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt21_createActionPerformed
        // TODO add your handling code here:
        String st1 = tf21_username.getText();
        String st2 = tf21_password.getText();
        
        if(st1!=null&&st2!=null)
        
        try{
		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                String url ="Jdbc:odbc:User";
		Connection conn = DriverManager.getConnection(url);
		Statement stt=conn.createStatement();
                PreparedStatement stmt = conn.prepareStatement("INSERT INTO User(name, password,account) VALUES (?, ?,?)");

                stmt.setString(1, st1);
                stmt.setString(2, st2);
                stmt.setInt(3, admin);
                
                stmt.executeUpdate();
                if(admin==1)JOptionPane.showMessageDialog(null,"Tạo người quản trị mới thành công");
                else JOptionPane.showMessageDialog(null,"Tạo người dùng thành công");
                
//                String sql="INSERT INTO User(name, password) VALUES(tf21_Username.getTxet(),tf21_Pass.getText())";
//                
//		 stt.executeUpdate(sql);
		
                } catch (Exception ex) {
                    System.out.println("Loi thao tac CSDL");
            Logger.getLogger(Form_QuanLyUser.class.getName()).log(Level.SEVERE, null, ex); 
              }
        else JOptionPane.showMessageDialog(null, "Ô tài khoản và ô mật khẩu không được để trống");
    }//GEN-LAST:event_bt21_createActionPerformed

    private void cb21_adminItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cb21_adminItemStateChanged
        // TODO add your handling code here:
        if(cb21_admin.isSelected())
            admin=1;
        else admin =0;
    }//GEN-LAST:event_cb21_adminItemStateChanged

    private void cb21_adminActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cb21_adminActionPerformed
        // TODO add your handling code here:
        if(cb21_admin.isSelected())
            admin=1;
        else admin =0;
    }//GEN-LAST:event_cb21_adminActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Form_QuanLyUser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Form_QuanLyUser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Form_QuanLyUser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Form_QuanLyUser.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Form_QuanLyUser().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt21_create;
    private javax.swing.JButton bt21_logout;
    private javax.swing.JCheckBox cb21_admin;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPasswordField tf21_password;
    private javax.swing.JTextField tf21_username;
    // End of variables declaration//GEN-END:variables
}
